package com.livesource.client.webservices.subversion;

import java.util.Date;

import com.google.gwt.json.client.JSONObject;
import com.livesource.client.model.entry.ProjectClass;
import com.livesource.client.model.entry.ProjectFeature;
import com.livesource.client.utilities.ConvertJsonp;
import com.livesource.client.utilities.FeatureNameGenerator;
import com.livesource.client.utilities.FileExtensions;

public class ConvertSubversionToProjectEntry {

	public static ProjectClass convertJsonp(JSONObject obj) {

		ProjectClass file = new ProjectClass();

		file.setFilePath(ConvertJsonp.convertToString(obj.get("filePath")));

		file.setFileName(ConvertJsonp.convertToString(obj.get("fileName")));

		if (obj.get("dateModified") != null) {

			file.setDateModified(new Date(new Long(ConvertJsonp
					.convertToNumber(obj.get("dateModified")))));
		}

		file.setAuthor(ConvertJsonp.convertToString(obj.get("fileAuthor")));

		file.setFileExtension(FileExtensions.getFileExtension(file
				.getFileName()));

		setEntryFeature(file, file.getFilePath(), file.getFileName());

		return file;
	}

	private static void setEntryFeature(ProjectFeature feature,
			String entryPath, String entryName) {

		feature.setFeaturePath(FeatureNameGenerator.getLastNameSpaced(
				entryPath, "/"));

		feature.setFeatureName(FeatureNameGenerator
				.getFileSpacedName(entryName));
	}
}
